package com.example.test.service.impl;

import com.example.test.service.IBaseService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public class BaseServiceImpl<E, I> implements IBaseService<E, I> {
    protected final JpaRepository<E, I> repository;

    public BaseServiceImpl(JpaRepository<E, I> repository) {
        this.repository = repository;
    }

    @Override
    public <S extends E> S save(S entity) {
        return repository.save(entity);
    }

    @Override
    public List<E> findAll() {
        return repository.findAll();
    }

    @Override
    public <S extends E> Optional<S> findOne(Example<S> example) {
        return repository.findOne(example);
    }

    @Override
    public E getOne(I id) {
        return repository.getOne(id);
    }

    @Override
    public Optional<E> findById(I id) {
        return repository.findById(id);
    }

    @Override
    public List<E> findAll(Sort sort) {
        return repository.findAll(sort);
    }

    @Override
    public Page<E> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public <S extends E> List<S> findAll(Example<S> example) {
        return repository.findAll(example);
    }

    @Override
    public <S extends E> List<S> findAll(Example<S> example, Sort sort) {
        return repository.findAll(example, sort);
    }

    @Override
    public <S extends E> Page<S> findAll(Example<S> example, Pageable pageable) {
        return repository.findAll(example, pageable);
    }

    @Override
    public List<E> findAllById(Iterable<I> ids) {
        return repository.findAllById(ids);
    }

    @Override
    public <S extends E> List<S> saveAll(Iterable<S> entities) {
        return repository.saveAll(entities);
    }

    @Override
    public <S extends E> S saveAndFlush(S entity) {
        return repository.saveAndFlush(entity);
    }

    @Override
    public void deleteInBatch(Iterable<E> entities) {
        repository.deleteInBatch(entities);
    }

    @Override
    public void deleteAllInBatch() {
        repository.deleteAllInBatch();
    }

    @Override
    public boolean existsById(I id) {
        return repository.existsById(id);
    }

    @Override
    public <S extends E> boolean exists(Example<S> example) {
        return repository.exists(example);
    }

    @Override
    public long count() {
        return repository.count();
    }

    @Override
    public <S extends E> long count(Example<S> example) {
        return repository.count(example);
    }

    @Override
    public void deleteById(I id) {
        repository.deleteById(id);
    }

    @Override
    public void delete(E entity) {
        repository.delete(entity);
    }

    @Override
    public void deleteAll(Iterable<? extends E> entities) {
        repository.deleteAll(entities);
    }

    @Override
    public void deleteAll() {
        repository.deleteAll();
    }
}
